/**数据验证*/
var ResidentsValidator = function() {  
    var handleSubmit = function() { 
        $('.form-horizontal').validate({  
            errorElement : 'span',  
            errorClass : 'help-block',  
            focusInvalid : false,  
            rules : {  
            	"rname" : {  
                    required : true  
                },  
                "icard" : {  
                    required : true  
                },
                "tel"  :{
                	required : true
                },
                "rnum"  :{
                	required : true
                }
            },  
            messages : {  
            	"rname"  : {  
                    required : "请输入户主姓名."  
                },  
                "icard" : {  
                    required : "请输入户主身份证号."  
                },
                "tel"  :{
                	required : "请输入联系电话."
                },
                "rnum"  :{
                	required : "请输入家庭人口数."
                }
            },  
  
            highlight : function(element) { 
                $(element).closest('.control-group').addClass('error');  
            },  
  
            success : function(label) { 
                label.closest('.control-group').removeClass('error');  
                label.remove();
            },  
  
            errorPlacement : function(error, element) {  
            	element.parent('div').append(error);  
            },  
  
            submitHandler : function(form) {
                form.submit();
            }  
        });  
  
        $('.form-horizontal input').keypress(function(e) {  
            if (e.which == 13) {  
                if ($('.form-horizontal').validate().form()) {  
                    $('.form-horizontal').submit();  
                }  
                return false;  
            }  
        });  
    }  
    return {  
        init : function() { 
            handleSubmit(); 
        }  
    };  
  
}(); 


/**保存数据*/
function saveResidents(){
	$('#myModal').modal('show');
	//住户信息
	var rcode=$("#rcode").val();
	var rid=$("#rid").val();
	var state=$("input[@type=radio][name=state][checked]").val();
	var rname=$("#rname").val();
	var sex=$("input[@type=radio][name=sex][checked]").val();
	var icard=$("#icard").val();
	var worktime=$("#worktime").val();
	var tel=$("#tel").val();
	var txtime=$("#txtime").val();
	var hpid=$("#hpid").val();
	var company=$("#company").val();
	var cqqk=$("#cqqk").val();
	var gfk=$("#gfk").val();
	var rnum=$("#rnum").val();
	var content=$("#ricontent").val();
	//水电指标
	var tidobjs=$("select[name=tid]");
	var rminobjs=$("input[name=rmin]");
	var rmaxobjs=$("input[name=rmax]");
	var priceobjs=$("input[name=price]");
	var tids='';
	var rmins='';
	var rmaxs='';
	var prices='';
	for(var i=0;i<tidobjs.length;i++){
		tids=tids+$(tidobjs[i]).val()+',';
		rmins=rmins+$(rminobjs[i]).val()+',';
		rmaxs=rmaxs+$(rmaxobjs[i]).val()+',';
		prices=prices+$(priceobjs[i]).val()+',';
	}
	//配偶信息
	var mid=$("#mid").val();
	var mname=$("#mname").val();
	var msex=$("input[@type=radio][name=msex][checked]").val();
	var micard=$("#micard").val();
	var mworktime=$("#mworktime").val();
	var mtel=$("#mtel").val();
	var mtxtime=$("#mtxtime").val();
	var mcompany=$("#mcompany").val();
	$.post("../base/save_Residents.action",{"residents.rcode":rcode,"residents.room.rid":rid,"residents.property.hpid":hpid,
		"residents.rname":rname,"residents.sex":sex,"residents.icard":icard,"residents.tel":tel,"residents.worktime":worktime,
		"residents.txtime":txtime,"residents.company":company,"residents.cqqk":cqqk,"residents.gfk":gfk,"residents.content":content,
		"residents.state":state,"residents.rnum":rnum,"tids":tids,"rmins":rmins,"rmaxs":rmaxs,"prices":prices,
		"member.mid":mid,"member.mname":mname,"member.sex":msex,"member.icard":micard,"member.tel":mtel,"member.worktime":mworktime,"member.txtime":mtxtime,"member.company":company}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("住户信息维护成功，如果该用户是合用电表或水表，请及时维护电表及水表的分摊规则.");
			$('#myinfo').modal('show');
			$("#closewin").bind("click",function(){
				window.location.href="../base/residents.action";
			});
		}else{
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
};
////////添加一行、删除一行封装方法///////
/**
 * 为table指定行添加一行
 *
 * tab 表id
 * row 行数，如：0->第一行 1->第二行 -2->倒数第二行 -1->最后一行
 * trHtml 添加行的html代码
 *
 */
function addTr(tab, row, trHtml){
   //获取table最后一行 $("#tab tr:last")
   //获取table第一行 $("#tab tr").eq(0)
   //获取table倒数第二行 $("#tab tr").eq(-2)
   var $tr=$("#"+tab+" tr:last");
   if($tr.size()==0){
      alert("指定的table id或行数不存在！");
      return;
   }
   $tr.after(trHtml);
};
 
function delTr(ckb){
   //获取选中的复选框，然后循环遍历删除
   var ckbs=$("input[name="+ckb+"]:checked");
   if(ckbs.size()==0){
      alert("要删除指定行，需选中要删除的行！");
      return;
   }
	 var i=0;
   ckbs.each(function(i,ckb){
		$("#dynamictable tr").eq($(ckb).val()-i).remove();
		i++;
   });
};


 
function addTr2(tab, row){
  var trHtml="<tr><td><input type='checkbox' id='inlineCheckbox1'  name='ckb' value='"+($("#dynamictable").find("tr").length)+"'></td><td>"+$("#dynamictable").find("tr").length+"</td>"+
			   "<td class='center'><select name='tid'><option value='001'>水</option><option value='002'>电</option></select></td>"+
			   "<td class='center'><input class='input-small focused' name='rmin' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' name='rmax' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' name='price' type='text' value=''></td></tr>";
  addTr(tab, row, trHtml);
};
 
function delTr2(){
// alert($("#dynamictable").find("tr").length);
   delTr('ckb');
	 $("#dynamictable").find("tr").each(function(i,trobj){
	    if(i>0){
		   $(trobj).find("td").eq(0).find("input").eq(0).val(i);
		   $(trobj).find("td").eq(1).html(i);
		}
	 });
};

$(document).ready(function(){
	ResidentsValidator.init();
});
